﻿using Coolzon.Common;
using Coolzon.UI.Core.Converters;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace Coolzon.UI.Core
{
    public class ConverterFactory
    {
        private static Dictionary<Type, ValueConverter> s_converterDictionary;
        
        //private static UnityContainer _valueConvertorsContainer;
        //private static UnityContainer _valueEditorsContainer;
        /// <summary>
        /// 初始化，预加载转换器
        /// </summary>
        public static void Init(string configFilePath)
        {
            //if (_valueConvertorsContainer == null)
            //{
            //    _valueConvertorsContainer = new UnityContainer();
            //    _valueEditorsContainer = new UnityContainer();

            //    ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
            //    fileMap.ExeConfigFilename = configFilePath;

            //    System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            //    UnityConfigurationSection section = (UnityConfigurationSection)config.GetSection("XProperty");

            //    section.Configure(_valueConvertorsContainer, "ValueConvertors");
            //    section.Configure(_valueEditorsContainer, "ValueEditors");
            //}
            //else
            //{
            //    throw new Exception("has inited");
            //}
        }

        /// <summary>
        /// 获取值转换器
        /// </summary>
        /// <param name="valueType"></param>
        /// <returns></returns>
        public static ValueConverter GetConverter(Type valueType)
        {
            foreach (var propertyType in PartLibraryManager.ConverterTypeDict.Keys)
            {
                if(propertyType == valueType)
                {
                    Type converterType = PartLibraryManager.ConverterTypeDict[propertyType];
                    return converterType.Assembly.CreateInstance(converterType.FullName) as ValueConverter;
                }
            }
            return null;

            //string valueTypeString = valueType.FullName;
            //try
            //{
            //    if (_valueConvertorsContainer == null)
            //    {
            //        Init(string.Format("{0}\\Config\\XProperty.xml",
            //        AppDomain.CurrentDomain.BaseDirectory));
            //    }
            //    if (_valueConvertorsContainer.IsRegistered<IPropertyValueConverter>(valueTypeString))
            //    {
            //        return _valueConvertorsContainer.Resolve<IPropertyValueConverter>(valueTypeString);
            //    }
            //    else
            //    {
            //        Logger.ErrorToTag("PropertyValueConverterFactory", string.Format("GetConverter转换器解析失败,FullName：{0}", valueTypeString));
            //        return null;
            //    }
            //}
            //catch(Exception ex)
            //{
            //    Logger.ErrorToTag("PropertyValueConverterFactory", string.Format("GetConverter转换器解析失败,FullName：{0},ex:{1}", valueTypeString, ex));
            //    return null;
            //}
        }

        /// <summary>
        /// 获取值的编辑器
        /// </summary>
        /// <param name="valueType"></param>
        /// <returns></returns>
        public static XPropertyValueEditor GetEditor(Type valueType)
        {
            return null;

            //string valueTypeString = valueType.FullName;
            //try
            //{
            //    if (_valueEditorsContainer == null)
            //    {
            //        Init(string.Format("{0}\\Config\\XProperty.xml",
            //        AppDomain.CurrentDomain.BaseDirectory));
            //    }
            //    if (_valueEditorsContainer.IsRegistered<XPropertyValueEditor>(valueTypeString))
            //    {
            //        return _valueEditorsContainer.Resolve<XPropertyValueEditor>(valueTypeString);
            //    }
            //    else
            //    {
            //        return new Editors.XDefaultErrorEditor(valueTypeString);
            //    }
            //}
            //catch (Exception ex)
            //{
            //    Logger.ErrorToTag("PropertyValueConverterFactory", string.Format("GetEditor转换器解析失败,FullName：{0},ex:{1}", valueTypeString, ex));
            //    return new Editors.XDefaultErrorEditor(valueTypeString);
            //}
        }
    }
}
